/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package blms.domain;

import static java.util.Collections.unmodifiableSet;

import java.util.HashSet;
import java.util.Set;

import blms.domain.Expression.Context;
import blms.exception.UnknownVariableException;
import blms.util.BLMSMessages;

/**
 * A {@link LeagueContext} object maps a variable name to Integer object holding
 * value. It provides two variables to an {@link Expression} to calculate player
 * standing in current season. The available variables are "seasonWins" and
 * "seasonLosses".
 * 
 * @see Context
 * 
 * @author João Arthur
 * @author Jonhnny Weslley
 * @author Thiago Emmanuel
 * @version 1.00, 25/04/2008
 * @since 1.0
 */
public class LeagueContext implements Context {

	private static final String SEASON_WINS = "seasonWins";
	private static final String SEASON_LOSSES = "seasonLosses";

	/**
	 * The set of the available variables from a {@link LeagueContext} instance.
	 */
	public static final Set<String> AVAILABLE_VARIABLES;
	
	static {
		final Set<String> vars = new HashSet<String>();
		vars.add(SEASON_WINS);
		vars.add(SEASON_LOSSES);
		AVAILABLE_VARIABLES = unmodifiableSet(vars);
	}

	private final User player;
	private final League league;

	/**
	 * Constructs a new <code>LeagueContext</code>.
	 * 
	 * @param league
	 *            a league to create the context.
	 * @param player
	 *            a user to create the context.
	 */
	public LeagueContext(final League league, final User player) {
		this.league = league;
		this.player = player;
	}

	/**
	 * Returns a {@link Set} of the variable names contained in this context.
	 * The available variables are "seasonWins" and "seasonLosses".
	 */
	public Set<String> getAvailableVariables() {
		return AVAILABLE_VARIABLES;
	}

	/**
	 * Returns the value to which the specified variable name is associated or
	 * throws an exception if the variable does not exist.
	 * 
	 * @throws RuntimeException
	 *             if the specified variable does not exist.
	 */
	public int getVariable(final String name) {
		if (SEASON_WINS.equals(name)) {
			return league.getNumberOfWins(player);

		} else if (SEASON_LOSSES.equals(name)) {
			return league.getNumberOfLosses(player);
		}
		throw new UnknownVariableException(BLMSMessages.UNKNOWN_STANDING_VARIABLE);
	}

}